﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FOModels.BlogNews;
using DataAccess.BlogNews;
using System.Data;
namespace Bussiness.BlogNews
{
    public class BlogNewsCategoryBLL
    {
        private BlogNewsCategoryDAL dal;
        public BlogNewsCategoryBLL()
        {
            dal = new BlogNewsCategoryDAL();
        }
        public BlogNewsCategoryModels GetTop1BlogNewsCategory()
        {
            BlogNewsCategoryModels fur = new BlogNewsCategoryModels();
            DataTable tb = dal.GetTop1BlogNewsCategory();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.BlogNewsCategoryID = int.Parse(row["BlogNewsCategoryID"].ToString()); 
                }
            }
            return fur;
        }
        public BlogNewsCategoryModels getBlogNewsCategoryInfo(int parentid, int langid)
        {
            BlogNewsCategoryModels fur = new BlogNewsCategoryModels();
            DataTable tb = dal.getBlogNewsCategoryInfo(parentid, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {

                    fur.BlogNewsCategoryID = int.Parse(row["BlogNewsCategoryID"].ToString());
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.BlogNewsCategoryName = row["BlogNewsCategoryName"].ToString();
                    if (row["ParentID"].ToString() != "")
                    {
                        fur.ParentID = int.Parse(row["ParentID"].ToString());
                    }
                    else
                    {
                        fur.ParentID = null;
                    }
                }
            }
            return fur;
        }
        public List<BlogNewsCategoryModels> getallBlogNewsCategory(int langid)
        {
            List<BlogNewsCategoryModels> list = new List<BlogNewsCategoryModels>();
            BlogNewsCategoryModels fur;
            DataTable tb = dal.getallBlogNewsCategory(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new BlogNewsCategoryModels();
                    fur.BlogNewsCategoryID = int.Parse(row["BlogNewsCategoryID"].ToString());
                    fur.FlagImage = row["FlagImage"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.BlogNewsCategoryName = row["BlogNewsCategoryName"].ToString();
                    if (row["ParentID"].ToString() != "")
                    {
                        fur.ParentID = int.Parse(row["ParentID"].ToString());
                        BlogNewsCategoryModels blognews = getBlogNewsCategoryInfo(int.Parse(row["ParentID"].ToString()), langid);
                        fur.ParentName = blognews.BlogNewsCategoryName;
                    }
                    else
                    {
                        fur.ParentID = null;
                    }
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<BlogNewsCategoryModels> gettop3catebynews(int langid)
        {
            List<BlogNewsCategoryModels> list = new List<BlogNewsCategoryModels>();
            BlogNewsCategoryModels fur;
            DataTable tb = dal.gettop3catebynews(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new BlogNewsCategoryModels();
                    fur.BlogNewsCategoryID = int.Parse(row["BlogNewsCategoryID"].ToString());
                    fur.BlogNewsCategoryName = row["BlogNewsCategoryName"].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        // Lấy danh sách cấp cha enable
        public List<BlogNewsCategoryModels> ResposiBlogNewsCategoryIsNullEnable(int langid)
        {
            List<BlogNewsCategoryModels> list = new List<BlogNewsCategoryModels>();
            BlogNewsCategoryModels fur;
            DataTable tb = dal.ResposiBlogNewsCategoryIsNullEnable(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new BlogNewsCategoryModels();
                    fur.BlogNewsCategoryID = int.Parse(row["BlogNewsCategoryID"].ToString());
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.BlogNewsCategoryName = row["BlogNewsCategoryName"].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        // Lấy danh sách cấp cha
        public List<BlogNewsCategoryModels> ResposiBlogNewsCategory(int langid)
        {
            List<BlogNewsCategoryModels> list = new List<BlogNewsCategoryModels>();
            BlogNewsCategoryModels fur;
            DataTable tb = dal.ResposiBlogNewsCategory(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new BlogNewsCategoryModels();
                    fur.BlogNewsCategoryID = int.Parse(row["BlogNewsCategoryID"].ToString());
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.BlogNewsCategoryName = row["BlogNewsCategoryName"].ToString();
                    if (row["ParentID"].ToString() != "")
                    {
                        fur.ParentID = int.Parse(row["ParentID"].ToString());
                        BlogNewsCategoryModels blognews = getBlogNewsCategoryInfo(langid, int.Parse(row["ParentID"].ToString()));
                        fur.ParentName = blognews.BlogNewsCategoryName;
                    }
                    else
                    {
                        fur.ParentID = null;
                    }
                    list.Add(fur);
                }
            }
            return list;
        }

        // Lấy danh sách để thêm tin tức
        public List<BlogNewsCategoryModels> ResposiBlogNewsCategoryManager(int langid)
        {
            List<BlogNewsCategoryModels> list = new List<BlogNewsCategoryModels>();
            BlogNewsCategoryModels fur;
            DataTable tb = dal.ResposiBlogNewsCategoryManager(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new BlogNewsCategoryModels();
                    fur.BlogNewsCategoryID = int.Parse(row["BlogNewsCategoryID"].ToString());
                    fur.FlagImage = row["FlagImage"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.BlogNewsCategoryName = row["BlogNewsCategoryName"].ToString();
                    if (row["ParentID"].ToString() != "")
                    {
                        fur.ParentID = int.Parse(row["ParentID"].ToString());
                        BlogNewsCategoryModels blognews = getBlogNewsCategoryInfo(langid, int.Parse(row["ParentID"].ToString()));
                        fur.ParentName = blognews.BlogNewsCategoryName;
                    }
                    else
                    {
                        fur.ParentID = null;
                    }
                    list.Add(fur);
                }
            }
            return list;
        }
        // Lấy danh sách blog news cate de xuat ra giao dien enbale =1
        public List<BlogNewsCategoryModels> ResposiBlogNewsCategoryManagerExView(int langid)
        {
            List<BlogNewsCategoryModels> list = new List<BlogNewsCategoryModels>();
            BlogNewsCategoryModels fur;
            DataTable tb = dal.ResposiBlogNewsCategoryManagerExView(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new BlogNewsCategoryModels();
                    fur.BlogNewsCategoryID = int.Parse(row["BlogNewsCategoryID"].ToString());
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.BlogNewsCategoryName = row["BlogNewsCategoryName"].ToString();
               
                    fur.ParentID = int.Parse(row["ParentID"].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        // Lấy danh sách blog news cate de xuat ra giao dien enbale =1
        public List<BlogNewsCategoryModels> ResposiBlogNewsCategoryManagerExViewByCate(int BlogNewsCategoryID, int langid)
        {
            List<BlogNewsCategoryModels> list = new List<BlogNewsCategoryModels>();
            BlogNewsCategoryModels fur;
            DataTable tb = dal.ResposiBlogNewsCategoryManagerExViewByCate(BlogNewsCategoryID,langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new BlogNewsCategoryModels();
                    fur.BlogNewsCategoryID = int.Parse(row["BlogNewsCategoryID"].ToString());
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.BlogNewsCategoryName = row["BlogNewsCategoryName"].ToString();
                    fur.ParentID = int.Parse(row["ParentID"].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public int UpdateBlogNewsCategory(BlogNewsCategoryModels models, int langid)
        {
            if (dal.UpdateBlogNewsCategory(models) > 0 && dal.UpdateBlogNewsCategoryLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertBlogNewsCategory(BlogNewsCategoryModels models)
        {
            if (dal.InsertBlogNewsCategory(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertBlogNewsCategoryLang(BlogNewsCategoryModels models, int langid)
        {
            if (dal.InsertBlogNewsCategoryLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteBlogNewsCategory(int CarLoanID)
        {
            if (dal.DeleteBlogNewsCategory(CarLoanID) > 0 && dal.DeleteBlogNewsCategoryLang(CarLoanID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }
}
